<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">

<title>class Tempfile - tempfile: Ruby Standard Library Documentation</title>


<script src="./js/navigation.js" defer></script>
<script src="./js/search.js" defer></script>
<script src="./js/search_index.js" defer></script>
<script src="./js/searcher.js" defer></script>
<script src="./js/darkfish.js" defer></script>

<script src="./js/jquery-3.2.0.min.js"></script>

<script src="./js/vue.min.js"></script>
<script src="./js/js.cookie.min.js"></script>

<link href="./css/fonts.css" rel="stylesheet">
<link id='rdoccss' href="./css/rdoc.css" rel="stylesheet">
<link href="./css/carbon17.css" rel="stylesheet">

<script type="text/javascript">
  var rdoc_rel_prefix = "./";
  var index_rel_prefix = "./";
  var darkModeCsseHref = "./css/rdoc-dm.css"
  var defaultModeCssHref = "./css/rdoc.css"
  // var cssDarkmode = Cookies.get('darkmode');
  
  if( Cookies.get("darkmode") == "true") {
	$('#rdoccss').attr("href", darkModeCsseHref);
}

//  https://cssdeck.com/blog/simple-jquery-stylesheet-switcher/

document.write('<style type="text/css">body{display:none}</style>');

</script>


</head>
<body id="top" role="document" class="class">
  <!-- this is class.html -->

  <div id='actionbar' >
    <div class='wrapper mdiv'>
      <ul class='grids g0'></ul>
    </div> 
    <!-- VERSION HEADER for 3.3.0.preview2 NOT FOUND -->
  </div> <!-- end action bar -->

  <div class='wrapper hdiv'>

    


    <nav id='vapp' role="navigation">
    <div id="project-navigation">
      <div id="home-section" role="region" title="Quick navigation" class="nav-section">
  <h2><a href="./index.html" rel="home">Home</a></h2>

  <div id="table-of-contents-navigation"  >
    <a href="./table_of_contents.html#pages">Pages</a>
    <a href="./table_of_contents.html#classes">Classes</a>
    <a href="./table_of_contents.html#methods">Methods</a>
  </div>
</div>

      <div id="search-section" role="search" class="project-section initially-hidden">
  <form action="#" method="get" accept-charset="utf-8">
    <div id="search-field-wrapper">
      <input id="search-field" role="combobox" aria-label="Search"
             aria-autocomplete="list" aria-controls="search-results"
             type="text" name="search" placeholder="Search" spellcheck="false"
             title="Type to search, Up and Down to navigate, Enter to load">
    </div>

    <ul id="search-results" aria-label="Search Results"
        aria-busy="false" aria-expanded="false"
        aria-atomic="false" class="initially-hidden"></ul>
  </form>
</div>

    </div>


    
<div class="nav-section">
  <h3>Table of Contents</h3>

  <ul class="link-list" role="directory">
    <li><a href="#class-Tempfile-label-Synopsis">Synopsis</a>
    <li><a href="#class-Tempfile-label-Good+practices">Good practices</a>
    <li><a href="#class-Tempfile-label-Explicit+close">Explicit close</a>
    <li><a href="#class-Tempfile-label-Unlink+after+creation">Unlink after creation</a>
    <li><a href="#class-Tempfile-label-Minor+notes">Minor notes</a>
  </ul>
</div>


    <button id='toggleThing' @click="toggleNav()" >Show/hide navigation</button>
    <div :class="isOpen ? 'block' : 'hidden' " id='toggleMe'>
      <div id="class-metadata">
        
        
<div id="parent-class-section" class="nav-section">
  <h3>Parent</h3>

  <p class="link">DelegateClass(File)
</div>

        
        
        
<!-- Method Quickref -->
<div id="method-list-section" class="nav-section">
  <h3>Methods</h3>

  <ul class="link-list" role="directory">
    <li ><a href="#method-c-create">::create</a>
    <li class="calls-super" ><a href="#method-c-new">::new</a>
    <li ><a href="#method-c-open">::open</a>
    <li ><a href="#method-i-close">#close</a>
    <li ><a href="#method-i-close-21">#close!</a>
    <li ><a href="#method-i-delete">#delete</a>
    <li ><a href="#method-i-length">#length</a>
    <li ><a href="#method-i-open">#open</a>
    <li ><a href="#method-i-path">#path</a>
    <li ><a href="#method-i-size">#size</a>
    <li ><a href="#method-i-unlink">#unlink</a>
  </ul>
</div>

      </div>
     </div>
    </nav>


    <div id='extraz'><div class='adzbox-index'  >
      
     </div>         
    </div>

    <main role="main" aria-labelledby="class-Tempfile">
    <h1 id="class-Tempfile" class="class">
      class Tempfile
    </h1>

    <section class="description">
    
<p>A utility class for managing temporary files. When you create a <a href="Tempfile.html"><code>Tempfile</code></a> object, it will create a temporary file with a unique filename. A <a href="Tempfile.html"><code>Tempfile</code></a> objects behaves just like a File object, and you can perform all the usual file operations on it: reading data, writing data, changing its permissions, etc. So although this class does not explicitly document all instance methods supported by File, you can in fact call any File instance method on a <a href="Tempfile.html"><code>Tempfile</code></a> object.</p>

<h2 id="class-Tempfile-label-Synopsis">Synopsis<span><a href="#class-Tempfile-label-Synopsis">&para;</a> <a href="#top">&uarr;</a></span></h2>

<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">&#39;tempfile&#39;</span>

<span class="ruby-identifier">file</span> = <span class="ruby-constant">Tempfile</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">&#39;foo&#39;</span>)
<span class="ruby-identifier">file</span>.<span class="ruby-identifier">path</span>      <span class="ruby-comment"># =&gt; A unique filename in the OS&#39;s temp directory,</span>
               <span class="ruby-comment">#    e.g.: &quot;/tmp/foo.24722.0&quot;</span>
               <span class="ruby-comment">#    This filename contains &#39;foo&#39; in its basename.</span>
<span class="ruby-identifier">file</span>.<span class="ruby-identifier">write</span>(<span class="ruby-string">&quot;hello world&quot;</span>)
<span class="ruby-identifier">file</span>.<span class="ruby-identifier">rewind</span>
<span class="ruby-identifier">file</span>.<span class="ruby-identifier">read</span>      <span class="ruby-comment"># =&gt; &quot;hello world&quot;</span>
<span class="ruby-identifier">file</span>.<span class="ruby-identifier">close</span>
<span class="ruby-identifier">file</span>.<span class="ruby-identifier">unlink</span>    <span class="ruby-comment"># deletes the temp file</span>
</pre>

<h2 id="class-Tempfile-label-Good+practices">Good practices<span><a href="#class-Tempfile-label-Good+practices">&para;</a> <a href="#top">&uarr;</a></span></h2>

<h3 id="class-Tempfile-label-Explicit+close">Explicit close<span><a href="#class-Tempfile-label-Explicit+close">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>When a <a href="Tempfile.html"><code>Tempfile</code></a> object is garbage collected, or when the Ruby interpreter exits, its associated temporary file is automatically deleted. This means that it’s unnecessary to explicitly delete a <a href="Tempfile.html"><code>Tempfile</code></a> after use, though it’s a good practice to do so: not explicitly deleting unused Tempfiles can potentially leave behind a large number of temp files on the filesystem until they’re garbage collected. The existence of these temp files can make it harder to determine a new <a href="Tempfile.html"><code>Tempfile</code></a> filename.</p>

<p>Therefore, one should always call <a href="Tempfile.html#method-i-unlink"><code>unlink</code></a> or close in an ensure block, like this:</p>

<pre class="ruby"><span class="ruby-identifier">file</span> = <span class="ruby-constant">Tempfile</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">&#39;foo&#39;</span>)
<span class="ruby-keyword">begin</span>
   <span class="ruby-comment"># ...do something with file...</span>
<span class="ruby-keyword">ensure</span>
   <span class="ruby-identifier">file</span>.<span class="ruby-identifier">close</span>
   <span class="ruby-identifier">file</span>.<span class="ruby-identifier">unlink</span>   <span class="ruby-comment"># deletes the temp file</span>
<span class="ruby-keyword">end</span>
</pre>

<p><a href="Tempfile.html#method-c-create"><code>Tempfile.create</code></a> { … } exists for this purpose and is more convenient to use. Note that <a href="Tempfile.html#method-c-create"><code>Tempfile.create</code></a> returns a File instance instead of a <a href="Tempfile.html"><code>Tempfile</code></a>, which also avoids the overhead and complications of delegation.</p>

<pre class="ruby"><span class="ruby-constant">Tempfile</span>.<span class="ruby-identifier">create</span>(<span class="ruby-string">&#39;foo&#39;</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">file</span><span class="ruby-operator">|</span>
   <span class="ruby-comment"># ...do something with file...</span>
<span class="ruby-keyword">end</span>
</pre>

<h3 id="class-Tempfile-label-Unlink+after+creation">Unlink after creation<span><a href="#class-Tempfile-label-Unlink+after+creation">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>On POSIX systems, it’s possible to unlink a file right after creating it, and before closing it. This removes the filesystem entry without closing the file handle, so it ensures that only the processes that already had the file handle open can access the file’s contents. It’s strongly recommended that you do this if you do not want any other processes to be able to read from or write to the <a href="Tempfile.html"><code>Tempfile</code></a>, and you do not need to know the Tempfile’s filename either.</p>

<p>For example, a practical use case for unlink-after-creation would be this: you need a large byte buffer that’s too large to comfortably fit in RAM, e.g. when you’re writing a web server and you want to buffer the client’s file upload data.</p>

<p>Please refer to <a href="Tempfile.html#method-i-unlink"><code>unlink</code></a> for more information and a code example.</p>

<h2 id="class-Tempfile-label-Minor+notes">Minor notes<span><a href="#class-Tempfile-label-Minor+notes">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>Tempfile’s filename picking method is both thread-safe and inter-process-safe: it guarantees that no other threads or processes will pick the same filename.</p>

<p><a href="Tempfile.html"><code>Tempfile</code></a> itself however may not be entirely thread-safe. If you access the same <a href="Tempfile.html"><code>Tempfile</code></a> object from multiple threads then you should protect it with a mutex.</p>

    </section>

      <section id="5Buntitled-5D" class="documentation-section">


      <section class="constants-list">
      <header>
      <h3>Constants</h3>
      </header>
      <dl>
          <dt id="VERSION">VERSION
          <dd>
        </dl>
        </section>



                <section id="public-class-5Buntitled-5D-method-details" class="method-section">
                <header>
                <h3>Public Class Methods</h3>
                </header>

                  <div id="method-c-create" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">create</span><span
                                class="method-args">(basename=&quot;&quot;, tmpdir=nil, mode: 0, **options) { |tmpfile| ... }</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Creates a file in the underlying file system; returns a new File object based on that file.</p>

<p>With no block given and no arguments, creates and returns file whose:</p>
<ul><li>
<p>Class is File (not Tempfile).</p>
</li><li>
<p>Directory is the system temporary directory (system-dependent).</p>
</li><li>
<p>Generated filename is unique in that directory.</p>
</li><li>
<p>Permissions are <code>0600</code>; see File.</p>
</li><li>
<p>Mode is <code>&#39;w+&#39;</code> (read/write mode, positioned at the end).</p>
</li></ul>

<p>With no block, the file is not removed automatically, and so should be explicitly removed.</p>

<p>Example:</p>

<pre class="ruby"><span class="ruby-identifier">f</span> = <span class="ruby-constant">Tempfile</span>.<span class="ruby-identifier">create</span>     <span class="ruby-comment"># =&gt; #&lt;File:/tmp/20220505-9795-17ky6f6&gt;</span>
<span class="ruby-identifier">f</span>.<span class="ruby-identifier">class</span>                 <span class="ruby-comment"># =&gt; File</span>
<span class="ruby-identifier">f</span>.<span class="ruby-identifier">path</span>                  <span class="ruby-comment"># =&gt; &quot;/tmp/20220505-9795-17ky6f6&quot;</span>
<span class="ruby-identifier">f</span>.<span class="ruby-identifier">stat</span>.<span class="ruby-identifier">mode</span>.<span class="ruby-identifier">to_s</span>(<span class="ruby-value">8</span>)     <span class="ruby-comment"># =&gt; &quot;100600&quot;</span>
<span class="ruby-constant">File</span>.<span class="ruby-identifier">exist?</span>(<span class="ruby-identifier">f</span>.<span class="ruby-identifier">path</span>)     <span class="ruby-comment"># =&gt; true</span>
<span class="ruby-constant">File</span>.<span class="ruby-identifier">unlink</span>(<span class="ruby-identifier">f</span>.<span class="ruby-identifier">path</span>)
<span class="ruby-constant">File</span>.<span class="ruby-identifier">exist?</span>(<span class="ruby-identifier">f</span>.<span class="ruby-identifier">path</span>)     <span class="ruby-comment"># =&gt; false</span>
</pre>

<p>Argument <code>basename</code>, if given, may be one of:</p>
<ul><li>
<p>A string: the generated filename begins with <code>basename</code>:</p>

<pre class="ruby"><span class="ruby-constant">Tempfile</span>.<span class="ruby-identifier">create</span>(<span class="ruby-string">&#39;foo&#39;</span>) <span class="ruby-comment"># =&gt; #&lt;File:/tmp/foo20220505-9795-1gok8l9&gt;</span>
</pre>
</li><li>
<p>An array of two strings <code>[prefix, suffix]</code>: the generated filename begins with <code>prefix</code> and ends with <code>suffix</code>:</p>

<pre class="ruby"><span class="ruby-constant">Tempfile</span>.<span class="ruby-identifier">create</span>(<span class="ruby-node">%w/foo .jpg/</span>) <span class="ruby-comment"># =&gt; #&lt;File:/tmp/foo20220505-17839-tnjchh.jpg&gt;</span>
</pre>
</li></ul>

<p>With arguments <code>basename</code> and <code>tmpdir</code>, the file is created in directory <code>tmpdir</code>:</p>

<pre class="ruby"><span class="ruby-constant">Tempfile</span>.<span class="ruby-identifier">create</span>(<span class="ruby-string">&#39;foo&#39;</span>, <span class="ruby-string">&#39;.&#39;</span>) <span class="ruby-comment"># =&gt; #&lt;File:./foo20220505-9795-1emu6g8&gt;</span>
</pre>

<p>Keyword arguments <code>mode</code> and <code>options</code> are passed directly to method File.open:</p>
<ul><li>
<p>The value given with <code>mode</code> must be an integer, and may be expressed as the logical OR of constants defined in File::Constants.</p>
</li><li>
<p>For <code>options</code>, see Open Options.</p>
</li></ul>

<p>With a block given, creates the file as above, passes it to the block, and returns the block’s value; before the return, the file object is closed and the underlying file is removed:</p>

<pre class="ruby"><span class="ruby-constant">Tempfile</span>.<span class="ruby-identifier">create</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">file</span><span class="ruby-operator">|</span> <span class="ruby-identifier">file</span>.<span class="ruby-identifier">path</span> } <span class="ruby-comment"># =&gt; &quot;/tmp/20220505-9795-rkists&quot;</span>
</pre>

<p>Related: <a href="Tempfile.html#method-c-new"><code>Tempfile.new</code></a>.</p>

                              <div class="method-source-code" id="create-source">
            <pre><span class="ruby-comment"># File tempfile.rb, line 400</span>
<span class="ruby-keyword">def</span> <span class="ruby-constant">Tempfile</span>.<span class="ruby-identifier ruby-title">create</span>(<span class="ruby-identifier">basename</span>=<span class="ruby-string">&quot;&quot;</span>, <span class="ruby-identifier">tmpdir</span>=<span class="ruby-keyword">nil</span>, <span class="ruby-value">mode:</span> <span class="ruby-value">0</span>, <span class="ruby-operator">**</span><span class="ruby-identifier">options</span>)
  <span class="ruby-identifier">tmpfile</span> = <span class="ruby-keyword">nil</span>
  <span class="ruby-constant">Dir</span><span class="ruby-operator">::</span><span class="ruby-constant">Tmpname</span>.<span class="ruby-identifier">create</span>(<span class="ruby-identifier">basename</span>, <span class="ruby-identifier">tmpdir</span>, <span class="ruby-operator">**</span><span class="ruby-identifier">options</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">tmpname</span>, <span class="ruby-identifier">n</span>, <span class="ruby-identifier">opts</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">mode</span> <span class="ruby-operator">|=</span> <span class="ruby-constant">File</span><span class="ruby-operator">::</span><span class="ruby-constant">RDWR</span><span class="ruby-operator">|</span><span class="ruby-constant">File</span><span class="ruby-operator">::</span><span class="ruby-constant">CREAT</span><span class="ruby-operator">|</span><span class="ruby-constant">File</span><span class="ruby-operator">::</span><span class="ruby-constant">EXCL</span>
    <span class="ruby-identifier">opts</span>[<span class="ruby-value">:perm</span>] = <span class="ruby-value">0600</span>
    <span class="ruby-identifier">tmpfile</span> = <span class="ruby-constant">File</span>.<span class="ruby-identifier">open</span>(<span class="ruby-identifier">tmpname</span>, <span class="ruby-identifier">mode</span>, <span class="ruby-operator">**</span><span class="ruby-identifier">opts</span>)
  <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">block_given?</span>
    <span class="ruby-keyword">begin</span>
      <span class="ruby-keyword">yield</span> <span class="ruby-identifier">tmpfile</span>
    <span class="ruby-keyword">ensure</span>
      <span class="ruby-keyword">unless</span> <span class="ruby-identifier">tmpfile</span>.<span class="ruby-identifier">closed?</span>
        <span class="ruby-keyword">if</span> <span class="ruby-constant">File</span>.<span class="ruby-identifier">identical?</span>(<span class="ruby-identifier">tmpfile</span>, <span class="ruby-identifier">tmpfile</span>.<span class="ruby-identifier">path</span>)
          <span class="ruby-identifier">unlinked</span> = <span class="ruby-constant">File</span>.<span class="ruby-identifier">unlink</span> <span class="ruby-identifier">tmpfile</span>.<span class="ruby-identifier">path</span> <span class="ruby-keyword">rescue</span> <span class="ruby-keyword">nil</span>
        <span class="ruby-keyword">end</span>
        <span class="ruby-identifier">tmpfile</span>.<span class="ruby-identifier">close</span>
      <span class="ruby-keyword">end</span>
      <span class="ruby-keyword">unless</span> <span class="ruby-identifier">unlinked</span>
        <span class="ruby-keyword">begin</span>
          <span class="ruby-constant">File</span>.<span class="ruby-identifier">unlink</span> <span class="ruby-identifier">tmpfile</span>.<span class="ruby-identifier">path</span>
        <span class="ruby-keyword">rescue</span> <span class="ruby-constant">Errno</span><span class="ruby-operator">::</span><span class="ruby-constant">ENOENT</span>
        <span class="ruby-keyword">end</span>
      <span class="ruby-keyword">end</span>
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">tmpfile</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-new" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">new</span><span
                                class="method-args">(basename=&quot;&quot;, tmpdir=nil, mode: 0, **options)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Creates a file in the underlying file system; returns a new Tempfile object based on that file.</p>

<p>If possible, consider instead using <a href="Tempfile.html#method-c-create"><code>Tempfile.create</code></a>, which:</p>
<ul><li>
<p>Avoids the performance cost of delegation, incurred when <a href="Tempfile.html#method-c-new"><code>Tempfile.new</code></a> calls its superclass <code>DelegateClass(File)</code>.</p>
</li><li>
<p>Does not rely on a finalizer to close and unlink the file, which can be unreliable.</p>
</li></ul>

<p>Creates and returns file whose:</p>
<ul><li>
<p>Class is Tempfile (not File, as in <a href="Tempfile.html#method-c-create"><code>Tempfile.create</code></a>).</p>
</li><li>
<p>Directory is the system temporary directory (system-dependent).</p>
</li><li>
<p>Generated filename is unique in that directory.</p>
</li><li>
<p>Permissions are <code>0600</code>; see File Permissions.</p>
</li><li>
<p>Mode is <code>&#39;w+&#39;</code> (read/write mode, positioned at the end).</p>
</li></ul>

<p>The underlying file is removed when the Tempfile object dies and is reclaimed by the garbage collector.</p>

<p>Example:</p>

<pre class="ruby"><span class="ruby-identifier">f</span> = <span class="ruby-constant">Tempfile</span>.<span class="ruby-identifier">new</span> <span class="ruby-comment"># =&gt; #&lt;Tempfile:/tmp/20220505-17839-1s0kt30&gt;</span>
<span class="ruby-identifier">f</span>.<span class="ruby-identifier">class</span>               <span class="ruby-comment"># =&gt; Tempfile</span>
<span class="ruby-identifier">f</span>.<span class="ruby-identifier">path</span>                <span class="ruby-comment"># =&gt; &quot;/tmp/20220505-17839-1s0kt30&quot;</span>
<span class="ruby-identifier">f</span>.<span class="ruby-identifier">stat</span>.<span class="ruby-identifier">mode</span>.<span class="ruby-identifier">to_s</span>(<span class="ruby-value">8</span>)   <span class="ruby-comment"># =&gt; &quot;100600&quot;</span>
<span class="ruby-constant">File</span>.<span class="ruby-identifier">exist?</span>(<span class="ruby-identifier">f</span>.<span class="ruby-identifier">path</span>)   <span class="ruby-comment"># =&gt; true</span>
<span class="ruby-constant">File</span>.<span class="ruby-identifier">unlink</span>(<span class="ruby-identifier">f</span>.<span class="ruby-identifier">path</span>)   <span class="ruby-comment">#</span>
<span class="ruby-constant">File</span>.<span class="ruby-identifier">exist?</span>(<span class="ruby-identifier">f</span>.<span class="ruby-identifier">path</span>)   <span class="ruby-comment"># =&gt; false</span>
</pre>

<p>Argument <code>basename</code>, if given, may be one of:</p>
<ul><li>
<p>A string: the generated filename begins with <code>basename</code>:</p>

<pre class="ruby"><span class="ruby-constant">Tempfile</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">&#39;foo&#39;</span>) <span class="ruby-comment"># =&gt; #&lt;Tempfile:/tmp/foo20220505-17839-1whk2f&gt;</span>
</pre>
</li><li>
<p>An array of two strings <code>[prefix, suffix]</code>: the generated filename begins with <code>prefix</code> and ends with <code>suffix</code>:</p>

<pre class="ruby"><span class="ruby-constant">Tempfile</span>.<span class="ruby-identifier">new</span>(<span class="ruby-node">%w/foo .jpg/</span>) <span class="ruby-comment"># =&gt; #&lt;Tempfile:/tmp/foo20220505-17839-58xtfi.jpg&gt;</span>
</pre>
</li></ul>

<p>With arguments <code>basename</code> and <code>tmpdir</code>, the file is created in directory <code>tmpdir</code>:</p>

<pre class="ruby"><span class="ruby-constant">Tempfile</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">&#39;foo&#39;</span>, <span class="ruby-string">&#39;.&#39;</span>) <span class="ruby-comment"># =&gt; #&lt;Tempfile:./foo20220505-17839-xfstr8&gt;</span>
</pre>

<p>Keyword arguments <code>mode</code> and <code>options</code> are passed directly to method File.open:</p>
<ul><li>
<p>The value given with <code>mode</code> must be an integer, and may be expressed as the logical OR of constants defined in File::Constants.</p>
</li><li>
<p>For <code>options</code>, see Open Options.</p>
</li></ul>

<p>Related: <a href="Tempfile.html#method-c-create"><code>Tempfile.create</code></a>.</p>
                                <div class="method-calls-super">
                                  Calls superclass method
                                  
                              </div>

                              <div class="method-source-code" id="new-source">
            <pre><span class="ruby-comment"># File tempfile.rb, line 150</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">initialize</span>(<span class="ruby-identifier">basename</span>=<span class="ruby-string">&quot;&quot;</span>, <span class="ruby-identifier">tmpdir</span>=<span class="ruby-keyword">nil</span>, <span class="ruby-value">mode:</span> <span class="ruby-value">0</span>, <span class="ruby-operator">**</span><span class="ruby-identifier">options</span>)
  <span class="ruby-identifier">warn</span> <span class="ruby-string">&quot;Tempfile.new doesn&#39;t call the given block.&quot;</span>, <span class="ruby-value">uplevel:</span> <span class="ruby-value">1</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">block_given?</span>

  <span class="ruby-ivar">@unlinked</span> = <span class="ruby-keyword">false</span>
  <span class="ruby-ivar">@mode</span> = <span class="ruby-identifier">mode</span><span class="ruby-operator">|</span><span class="ruby-constant">File</span><span class="ruby-operator">::</span><span class="ruby-constant">RDWR</span><span class="ruby-operator">|</span><span class="ruby-constant">File</span><span class="ruby-operator">::</span><span class="ruby-constant">CREAT</span><span class="ruby-operator">|</span><span class="ruby-constant">File</span><span class="ruby-operator">::</span><span class="ruby-constant">EXCL</span>
  <span class="ruby-operator">::</span><span class="ruby-constant">Dir</span><span class="ruby-operator">::</span><span class="ruby-constant">Tmpname</span>.<span class="ruby-identifier">create</span>(<span class="ruby-identifier">basename</span>, <span class="ruby-identifier">tmpdir</span>, <span class="ruby-operator">**</span><span class="ruby-identifier">options</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">tmpname</span>, <span class="ruby-identifier">n</span>, <span class="ruby-identifier">opts</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">opts</span>[<span class="ruby-value">:perm</span>] = <span class="ruby-value">0600</span>
    <span class="ruby-ivar">@tmpfile</span> = <span class="ruby-constant">File</span>.<span class="ruby-identifier">open</span>(<span class="ruby-identifier">tmpname</span>, <span class="ruby-ivar">@mode</span>, <span class="ruby-operator">**</span><span class="ruby-identifier">opts</span>)
    <span class="ruby-ivar">@opts</span> = <span class="ruby-identifier">opts</span>.<span class="ruby-identifier">freeze</span>
  <span class="ruby-keyword">end</span>
  <span class="ruby-constant">ObjectSpace</span>.<span class="ruby-identifier">define_finalizer</span>(<span class="ruby-keyword">self</span>, <span class="ruby-constant">Remover</span>.<span class="ruby-identifier">new</span>(<span class="ruby-ivar">@tmpfile</span>))

  <span class="ruby-keyword">super</span>(<span class="ruby-ivar">@tmpfile</span>)
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-open" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">open</span><span
                                class="method-args">(*args, **kw) { |tempfile| ... }</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Creates a new <a href="Tempfile.html"><code>Tempfile</code></a>.</p>

<p>This method is not recommended and exists mostly for backward compatibility. Please use <a href="Tempfile.html#method-c-create"><code>Tempfile.create</code></a> instead, which avoids the cost of delegation, does not rely on a finalizer, and also unlinks the file when given a block.</p>

<p><a href="Tempfile.html#method-c-open"><code>Tempfile.open</code></a> is still appropriate if you need the <a href="Tempfile.html"><code>Tempfile</code></a> to be unlinked by a finalizer and you cannot explicitly know where in the program the <a href="Tempfile.html"><code>Tempfile</code></a> can be unlinked safely.</p>

<p>If no block is given, this is a synonym for <a href="Tempfile.html#method-c-new"><code>Tempfile.new</code></a>.</p>

<p>If a block is given, then a <a href="Tempfile.html"><code>Tempfile</code></a> object will be constructed, and the block is run with the <a href="Tempfile.html"><code>Tempfile</code></a> object as argument. The <a href="Tempfile.html"><code>Tempfile</code></a> object will be automatically closed after the block terminates. However, the file will <strong>not</strong> be unlinked and needs to be manually unlinked with <a href="Tempfile.html#method-i-close-21"><code>Tempfile#close!</code></a> or <a href="Tempfile.html#method-i-unlink"><code>Tempfile#unlink</code></a>. The finalizer will try to unlink but should not be relied upon as it can keep the file on the disk much longer than intended. For instance, on CRuby, finalizers can be delayed due to conservative stack scanning and references left in unused memory.</p>

<p>The call returns the value of the block.</p>

<p>In any case, all arguments (<code>*args</code>) will be passed to <a href="Tempfile.html#method-c-new"><code>Tempfile.new</code></a>.</p>

<pre class="ruby"><span class="ruby-constant">Tempfile</span>.<span class="ruby-identifier">open</span>(<span class="ruby-string">&#39;foo&#39;</span>, <span class="ruby-string">&#39;/home/temp&#39;</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">f</span><span class="ruby-operator">|</span>
   <span class="ruby-comment"># ... do something with f ...</span>
<span class="ruby-keyword">end</span>

<span class="ruby-comment"># Equivalent:</span>
<span class="ruby-identifier">f</span> = <span class="ruby-constant">Tempfile</span>.<span class="ruby-identifier">open</span>(<span class="ruby-string">&#39;foo&#39;</span>, <span class="ruby-string">&#39;/home/temp&#39;</span>)
<span class="ruby-keyword">begin</span>
   <span class="ruby-comment"># ... do something with f ...</span>
<span class="ruby-keyword">ensure</span>
   <span class="ruby-identifier">f</span>.<span class="ruby-identifier">close</span>
<span class="ruby-keyword">end</span>
</pre>

                              <div class="method-source-code" id="open-source">
            <pre><span class="ruby-comment"># File tempfile.rb, line 328</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">open</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">args</span>, <span class="ruby-operator">**</span><span class="ruby-identifier">kw</span>)
  <span class="ruby-identifier">tempfile</span> = <span class="ruby-identifier">new</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">args</span>, <span class="ruby-operator">**</span><span class="ruby-identifier">kw</span>)

  <span class="ruby-keyword">if</span> <span class="ruby-identifier">block_given?</span>
    <span class="ruby-keyword">begin</span>
      <span class="ruby-keyword">yield</span>(<span class="ruby-identifier">tempfile</span>)
    <span class="ruby-keyword">ensure</span>
      <span class="ruby-identifier">tempfile</span>.<span class="ruby-identifier">close</span>
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">tempfile</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                          </section>

                <section id="public-instance-5Buntitled-5D-method-details" class="method-section">
                <header>
                <h3>Public Instance Methods</h3>
                </header>

                  <div id="method-i-close" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">close</span><span
                                class="method-args">(unlink_now=false)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Closes the file. If <code>unlink_now</code> is true, then the file will be unlinked (deleted) after closing. Of course, you can choose to later call <a href="Tempfile.html#method-i-unlink"><code>unlink</code></a> if you do not unlink it now.</p>

<p>If you don’t explicitly unlink the temporary file, the removal will be delayed until the object is finalized.</p>

                              <div class="method-source-code" id="close-source">
            <pre><span class="ruby-comment"># File tempfile.rb, line 184</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">close</span>(<span class="ruby-identifier">unlink_now</span>=<span class="ruby-keyword">false</span>)
  <span class="ruby-identifier">_close</span>
  <span class="ruby-identifier">unlink</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">unlink_now</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-close-21" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">close!</span><span
                                class="method-args">()</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Closes and unlinks (deletes) the file. Has the same effect as called <code>close(true)</code>.</p>

                              <div class="method-source-code" id="close-21-source">
            <pre><span class="ruby-comment"># File tempfile.rb, line 191</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">close!</span>
  <span class="ruby-identifier">close</span>(<span class="ruby-keyword">true</span>)
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-delete" class="method-detail method-alias">
                            <div class="method-heading">
                              <span class="method-name">delete</span><span
                                class="method-args">()</span>
                            </div>

                            <div class="method-description">
                              

                            </div>


                            <div class="aliases">
                              Alias for: <a href="Tempfile.html#method-i-unlink">unlink</a>
                            </div>
                          </div>

                  <div id="method-i-length" class="method-detail method-alias">
                            <div class="method-heading">
                              <span class="method-name">length</span><span
                                class="method-args">()</span>
                            </div>

                            <div class="method-description">
                              

                            </div>


                            <div class="aliases">
                              Alias for: <a href="Tempfile.html#method-i-size">size</a>
                            </div>
                          </div>

                  <div id="method-i-open" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">open</span><span
                                class="method-args">()</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Opens or reopens the file with mode “r+”.</p>

                              <div class="method-source-code" id="open-source">
            <pre><span class="ruby-comment"># File tempfile.rb, line 166</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">open</span>
  <span class="ruby-identifier">_close</span>
  <span class="ruby-identifier">mode</span> = <span class="ruby-ivar">@mode</span> <span class="ruby-operator">&amp;</span> <span class="ruby-operator">~</span>(<span class="ruby-constant">File</span><span class="ruby-operator">::</span><span class="ruby-constant">CREAT</span><span class="ruby-operator">|</span><span class="ruby-constant">File</span><span class="ruby-operator">::</span><span class="ruby-constant">EXCL</span>)
  <span class="ruby-ivar">@tmpfile</span> = <span class="ruby-constant">File</span>.<span class="ruby-identifier">open</span>(<span class="ruby-ivar">@tmpfile</span>.<span class="ruby-identifier">path</span>, <span class="ruby-identifier">mode</span>, <span class="ruby-operator">**</span><span class="ruby-ivar">@opts</span>)
  <span class="ruby-identifier">__setobj__</span>(<span class="ruby-ivar">@tmpfile</span>)
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-path" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">path</span><span
                                class="method-args">()</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns the full path name of the temporary file. This will be nil if <a href="Tempfile.html#method-i-unlink"><code>unlink</code></a> has been called.</p>

                              <div class="method-source-code" id="path-source">
            <pre><span class="ruby-comment"># File tempfile.rb, line 244</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">path</span>
  <span class="ruby-ivar">@unlinked</span> <span class="ruby-operator">?</span> <span class="ruby-keyword">nil</span> <span class="ruby-operator">:</span> <span class="ruby-ivar">@tmpfile</span>.<span class="ruby-identifier">path</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-size" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">size</span><span
                                class="method-args">()</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns the size of the temporary file.  As a side effect, the IO buffer is flushed before determining the size.</p>

                              <div class="method-source-code" id="size-source">
            <pre><span class="ruby-comment"># File tempfile.rb, line 250</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">size</span>
  <span class="ruby-keyword">if</span> <span class="ruby-operator">!</span><span class="ruby-ivar">@tmpfile</span>.<span class="ruby-identifier">closed?</span>
    <span class="ruby-ivar">@tmpfile</span>.<span class="ruby-identifier">size</span> <span class="ruby-comment"># File#size calls rb_io_flush_raw()</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-constant">File</span>.<span class="ruby-identifier">size</span>(<span class="ruby-ivar">@tmpfile</span>.<span class="ruby-identifier">path</span>)
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>

                            <div class="aliases">
                              Also aliased as: <a href="Tempfile.html#method-i-length">length</a>
                            </div>

                          </div>

                  <div id="method-i-unlink" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">unlink</span><span
                                class="method-args">()</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Unlinks (deletes) the file from the filesystem. One should always unlink the file after using it, as is explained in the “Explicit close” good practice section in the <a href="Tempfile.html"><code>Tempfile</code></a> overview:</p>

<pre class="ruby"><span class="ruby-identifier">file</span> = <span class="ruby-constant">Tempfile</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">&#39;foo&#39;</span>)
<span class="ruby-keyword">begin</span>
   <span class="ruby-comment"># ...do something with file...</span>
<span class="ruby-keyword">ensure</span>
   <span class="ruby-identifier">file</span>.<span class="ruby-identifier">close</span>
   <span class="ruby-identifier">file</span>.<span class="ruby-identifier">unlink</span>   <span class="ruby-comment"># deletes the temp file</span>
<span class="ruby-keyword">end</span>
</pre>

<h3 id="method-i-unlink-label-Unlink-before-close">Unlink-before-close<span><a href="#method-i-unlink-label-Unlink-before-close">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>On POSIX systems it’s possible to unlink a file before closing it. This practice is explained in detail in the <a href="Tempfile.html"><code>Tempfile</code></a> overview (section “Unlink after creation”); please refer there for more information.</p>

<p>However, unlink-before-close may not be supported on non-POSIX operating systems. Microsoft Windows is the most notable case: unlinking a non-closed file will result in an error, which this method will silently ignore. If you want to practice unlink-before-close whenever possible, then you should write code like this:</p>

<pre class="ruby"><span class="ruby-identifier">file</span> = <span class="ruby-constant">Tempfile</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">&#39;foo&#39;</span>)
<span class="ruby-identifier">file</span>.<span class="ruby-identifier">unlink</span>   <span class="ruby-comment"># On Windows this silently fails.</span>
<span class="ruby-keyword">begin</span>
   <span class="ruby-comment"># ... do something with file ...</span>
<span class="ruby-keyword">ensure</span>
   <span class="ruby-identifier">file</span>.<span class="ruby-identifier">close!</span>   <span class="ruby-comment"># Closes the file handle. If the file wasn&#39;t unlinked</span>
                 <span class="ruby-comment"># because #unlink failed, then this method will attempt</span>
                 <span class="ruby-comment"># to do so again.</span>
<span class="ruby-keyword">end</span>
</pre>

                              <div class="method-source-code" id="unlink-source">
            <pre><span class="ruby-comment"># File tempfile.rb, line 228</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">unlink</span>
  <span class="ruby-keyword">return</span> <span class="ruby-keyword">if</span> <span class="ruby-ivar">@unlinked</span>
  <span class="ruby-keyword">begin</span>
    <span class="ruby-constant">File</span>.<span class="ruby-identifier">unlink</span>(<span class="ruby-ivar">@tmpfile</span>.<span class="ruby-identifier">path</span>)
  <span class="ruby-keyword">rescue</span> <span class="ruby-constant">Errno</span><span class="ruby-operator">::</span><span class="ruby-constant">ENOENT</span>
  <span class="ruby-keyword">rescue</span> <span class="ruby-constant">Errno</span><span class="ruby-operator">::</span><span class="ruby-constant">EACCES</span>
    <span class="ruby-comment"># may not be able to unlink on Windows; just ignore</span>
    <span class="ruby-keyword">return</span>
  <span class="ruby-keyword">end</span>
  <span class="ruby-constant">ObjectSpace</span>.<span class="ruby-identifier">undefine_finalizer</span>(<span class="ruby-keyword">self</span>)
  <span class="ruby-ivar">@unlinked</span> = <span class="ruby-keyword">true</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>

                            <div class="aliases">
                              Also aliased as: <a href="Tempfile.html#method-i-delete">delete</a>
                            </div>

                          </div>

                          </section>

              </section>
              </main>



            </div>  <!--  class='wrapper hdiv' -->


<footer id="validator-badges" role="contentinfo">
<p><a href="https://validator.w3.org/check/referer">Validate</a></p>
<p>Generated by <a href="https://ruby.github.io/rdoc/">RDoc</a> 6.4.0.</p>
<p>Based on <a href="https://github.com/ged/darkfish/">Darkfish</a> by <a href="http://deveiate.org">Michael Granger</a>.</p>

  
    <p><p><a href="https://ruby-doc.org">Ruby-doc.org</a> is provided by <a href="https://jamesbritt.com">James Britt</a> and <a href="https://neurogami.com">Neurogami</a>.</p><p><a href="https://jamesbritt.bandcamp.com/">Maximum R+D</a>.  </p>
</p>
  
  </footer>

<script type="text/javascript">


  let ads  = $("#carbonads-container").children().detach();


  function swapMode() {
    var cookieName = 'darkmode';
    var cssDarkmode = Cookies.get(cookieName);
    console.log("***** swapMode! " + cssDarkmode + " *****");


    if (cssDarkmode == "true") {
      console.log("We have dark mode, set the css to light ...");
      $('#rdoccss').attr("href", defaultModeCssHref);
      $('#cssSelect').text("Dark mode");
      cssDarkmode = "false";
      console.log("swapMode! Now set cookie to " + cssDarkmode);
      Cookies.set(cookieName, cssDarkmode);

    } else {
      console.log("We not have dark mode, set the css to dark ...");
      $('#rdoccss').attr("href", darkModeCsseHref);
      $('#cssSelect').text("Light mode");
      cssDarkmode = "true";
      console.log("swapMode! Now set cookie to " + cssDarkmode);
      Cookies.set(cookieName, cssDarkmode);

    }

    console.log("  --------------- ");
  }


const vueCssApp = new Vue({
el: '#menubar',
data: {
isDark: false
},
methods: {
toggleClass: function(event){
this.isDark = !this.isDark;
}
}
})

const vueApp = new Vue({
el: '#vapp',
data: { 
isOpen: true
},

mounted() {
this.handleResize();
this.manage_mob_classes();
window.addEventListener('resize', this.handleResize)
//this.isOpen !=  (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent));
},
destroyed() {
window.removeEventListener('resize', this.handleResize)
},
created() {
//manage_mob_classes();
},

methods : {
isMobile() {
  return (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent));
},

  handleResize() {
    if (!this.isMobile()) {
      this.isOpen = window.innerWidth > 800;
    }
  },

  manage_mob_classes() {
    if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
      $("nav").addClass("mob_nav");
      $("main").addClass("mob_main");
      $("#extraz").addClass("mob_extraz");
      $("#carbonads-container").addClass("mob_carbonads-container");
      this.isOpen  = false;
    } else {
      $("nav").removeClass("mob_nav") 
        $("main").removeClass("mob_main");
      $("#extraz").removeClass("mob_extraz");
      $("#carbonads-container").removeClass("mob_carbonads-container");
      this.isOpen  = true;
    }
  },

  toggleNav() {
    this.isOpen =! this.isOpen ;
    // alert("Toggle nav!");
    console.log("toggleNav() click: " + this.isOpen );
  }
}
})

$("#carbonads-container").append(ads);


$(function() {

    var darkmode = Cookies.get("darkmode");
    console.log("Document ready: " + darkmode);

    if ( darkmode  == "true" ) {
      $('#cssSelect').text("Light mode");
    } else {
      $('#cssSelect').text("Dark mode");
     }

    $('body').css('display','block');
    });

</script>

    
  </body> 
</html>

